package com.design.module.controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.design.framework.annotion.SysLog;
import com.design.framework.api.ApiResponse;
import com.design.framework.api.bean.ApiMap;
import com.design.framework.base.controller.BaseController;
import com.design.framework.cache.CachePool;
import com.design.framework.cache.service.RedisService;
import com.design.framework.enums.LogOperateEnums;
import com.design.framework.utils.DateUtil;
import com.design.framework.utils.JsonUtils;
import com.design.framework.utils.Md5Utils;
import com.design.framework.utils.SerializeUtil;
import com.design.framework.utils.TokenUtils;
import com.design.framework.validata.JsonValidation;
import com.design.framework.validata.bean.ValidationResult;
import com.design.module.entity.User;
import com.design.module.enums.ApiCodeEnum;
import com.design.module.service.UserService;

/**
 * 用户
 * @author John
 * @datatime 2018-09-11 16:54:18
 */
@Controller
@RequestMapping("/pc/user")
public class UserController extends BaseController<User,String>{
	
	@Resource
	private UserService userService;
	

	@Resource
	private RedisService redisService;

	@PostConstruct
	@Override
	public void initialize() {
		super.setBaseService(userService);
		
	}

	@SysLog(operate = LogOperateEnums.LOGIN, description = "PC-用户登录")
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public ApiMap login(User user, @RequestBody JSONObject jsonObject, ValidationResult validationResult)
			throws Exception {
		if (!JsonValidation.containsKeys(validationResult, jsonObject, "userPassword", "userCode")) {
			return ApiResponse.returnParamError("账户和密码不能为空");
		}
		user = JsonUtils.toBean(jsonObject, user);
		user.setUserPassword(Md5Utils.encrypByMd5(user.getUserPassword()));
		user = userService.login(user);
		if (user == null) {
			User phoneUser = new User();
			phoneUser.setUserPhone(jsonObject.getString("userCode"));
			phoneUser.setUserPassword(Md5Utils.encrypByMd5(jsonObject.getString("userPassword")));
			phoneUser = userService.login(phoneUser);
			if (phoneUser == null) {
				return ApiResponse.returnCustomCodeMsg(ApiCodeEnum.USER_OR_PWD_INCORRECT.getCode(),
						ApiCodeEnum.USER_OR_PWD_INCORRECT.getMessage());
			}
		}
		String token = TokenUtils.getToken();
		if (redisService != null) {
			redisService.set(token, user.getId(), 60 * 60 * 8);
			redisService.set(SerializeUtil.serialize(user.getId()), SerializeUtil.serialize(user));
		} else {
			CachePool.getInstance().putCacheItem(token, user.getId(), DateUtil.getHalfAnHourTime());
		}
		ApiResponse.addMap("token", token);
		ApiResponse.addMap("userName", user.getUserName());
		return ApiResponse.returnSucccssMap();
	}
}

